<template>
  <div class="mod-demo-echarts">
    <div>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-card>
            <div id="J_chartBarBox" class="chart-box"></div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    name: 'scountmaterial2',
    data () {
      return {
        dataForm: {
        },
        dataListLoading: false,
        title:'素材数量统计',
        names:[],
        values:[],
      }
    },
    components: {
    },
    mounted () {
      this.getDataList()
    },
    activated () {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
      if (this.chartBar) {
        this.chartBar.resize()
      }
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/sys/material/scountmaterial'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            var val = data.scount
            let keys = Object.keys(val)
            for(var i=0;i<keys.length;i++){
              this.names.push(keys[i])
              this.values.push(val[keys[i]])
            }
            this.init()
          }
          this.dataListLoading = false
        })
      },
      init(){
        console.log('names',this.names);
        console.log('values',this.values);
        var option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          title:{
            text:this.title
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data:this.names
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '素材数量',
              type: 'bar',
              data: this.values
            }
          ]
        }
        this.chartBar = echarts.init(document.getElementById('J_chartBarBox'))
        this.chartBar.setOption(option)
        window.addEventListener('resize', () => {
          this.chartBar.resize()
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      }
    }
  }
</script>
